package com.trt.eatmeat.test;

import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;

/**
 *  业务工具类
 */
public class HttpOAUtil {
    public static List<HttpCookie> cookie =new ArrayList<>();
    public static  String mac = IdUtil.fastUUID();


    public static String getCookie(){
        StringJoiner s =new StringJoiner(";");
        if(cookie!=null){
            for (HttpCookie c:cookie){
                s.add(c.getName()+"="+c.getValue());
            }
        }
        return s.toString();
    }
    /**
     * 内外请求get
     * @param url
     * @param paramMap
     * @return
     */
    public static Map<String,Object> innerGet(String url, Map<String, Object> paramMap){
        HttpResponse res=  HttpRequest.post(url)
                .header("Cookie",getCookie())
                .form(paramMap)//表单内容
                .timeout(20000)//超时，毫秒
                .execute();
        if(res.getCookies()!=null && res.getCookies().size()>0){
            HttpOAUtil.cookie = res.getCookies();
            HttpCookie c = new HttpCookie("__randcode__",mac);
            HttpOAUtil.cookie.add(c);
        }
        String response = res.body();
        Map<String,Object> result = new HashMap<>();
        if(JSONUtil.isJson(response)){
            result= JSONUtil.toBean(response,Map.class);
        }else {
            result.put("result",response);
        }
        return result;
    }

    /**
     * 内网请求post
     * @param url
     * @param paramMap
     * @return
     */
    public static Map<String,Object> innerPost(String url, Map<String, Object> paramMap){
        HttpResponse res=  HttpRequest.post(url)
                .header("Cookie",getCookie())
                .form(paramMap)//表单内容
                .timeout(20000)//超时，毫秒
                .execute();
        if(res.getCookies()!=null && res.getCookies().size()>0){
            HttpOAUtil.cookie = res.getCookies();
            HttpCookie c = new HttpCookie("__randcode__",mac);
            HttpOAUtil.cookie.add(c);
        }
        String response = res.body();
        Map<String,Object> result = new HashMap<>();
        if(JSONUtil.isJson(response)){
            result= JSONUtil.toBean(response,Map.class);
        }else {
            result.put("result",response);
        }
        return result;
    }

    /**
     * 外部请求
     * @param url
     * @param paramMap
     * @return
     */
    public static Map<String,Object> outPost(String url, Map<String, Object> paramMap){
        HttpResponse res=  HttpRequest.post(url)
                .header("Cookie",getCookie())
                .form(paramMap)//表单内容
                .timeout(20000)//超时，毫秒
                .execute();
        if(res.getCookies()!=null && res.getCookies().size()>0){
            HttpOAUtil.cookie = res.getCookies();
            HttpCookie c = new HttpCookie("__randcode__",mac);
            HttpOAUtil.cookie.add(c);
        }
        String response = res.body();
        Map<String,Object> result = new HashMap<>();
        if(JSONUtil.isJson(response)){
            result= JSONUtil.toBean(response,Map.class);
        }else {
            result.put("result",response);
        }
        return result;
    }

    public static Map<String,Object> outPostJson(String url, Map<String, Object> paramMap){
        String json = JSONUtil.toJsonStr(paramMap);
        String response = HttpRequest.post(url)
                .body(json)
                .execute().body();
        Map<String,Object> result = new HashMap<>();
        if(JSONUtil.isJson(response)){
            result= JSONUtil.toBean(response,Map.class);
        }else {
            result.put("result",response);
        }
        return result;
    }

}
